﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MOJsdk;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for Control_DuplicateFile.xaml
    /// </summary>
    public partial class Control_DuplicateFile : UserControl
    {
        private MOJFile _file1, _file2;
        private int _result = -1;

        public Control_DuplicateFile(MOJFile File1, MOJFile File2)
        {
            InitializeComponent();
            _file1 = File1;
            _file2 = File2;
            INI();
        }

        private void INI()
        {
            INIStyle();
            FillListBox(_listBox_item1, _file1);
            FillListBox(_listBox_item2, _file2);
            FillComboBox();
            INIEvents();
        }
        private void INIStyle()
        {
            _txt.Text = MOJLocalization.Localize("Duplicate files identified, please chose what to do");
            _btn_ok.Content = MOJLocalization.Localize("OK");
            _btn_Open1.Content = MOJLocalization.Localize("Open Folder");
            _btn_Open2.Content = MOJLocalization.Localize("Open Folder");

            _Grid.ApplyStyle("0101157");
            _txt.ApplyStyle("0101158");
            _listBox_item1.ApplyStyle("0101159");
            _listBox_item2.ApplyStyle("0101160");
            _cmb_action.ApplyStyle("0101161");
            _btn_ok.ApplyStyle("0101162");
            _btn_Open1.ApplyStyle("0101163");
            _btn_Open2.ApplyStyle("0101164");
        }
        private void FillListBox(ListBox list, MOJFile _file)
        {
            list.Items.Add(MOJLocalization.Localize("Original Path") + ":");
            list.Items.Add(_file.FileFullPath);
            list.Items.Add(MOJLocalization.Localize("Move To") + ":");
            list.Items.Add(_file.GetOrganizedPath());
            list.Items.Add("-------------------");

            if (_file.FileType == 0)
            {
                MovieFileInfo info = _file.Info;

                list.Items.Add(MOJLocalization.Localize("Movie File"));
                list.Items.Add(MOJLocalization.Localize("Index") + ": " + _file.FileIndex);
                list.Items.Add(MOJLocalization.Localize("Size") + ": " + info.FileSize);
                list.Items.Add(MOJLocalization.Localize("Duration") + ": " + info.Duration.TotalMinutes + " m");
                list.Items.Add(MOJLocalization.Localize("Format") + ": " + info.FileFormat);
                list.Items.Add(MOJLocalization.Localize("FrameRate") + ": " + info.FrameRate);
                list.Items.Add(MOJLocalization.Localize("Resolution") + ": " + info.Resolution);
                list.Items.Add(MOJLocalization.Localize("Audio Codec") + ": " + info.AudioCodec);
                list.Items.Add(MOJLocalization.Localize("Video Codec") + ": " + info.VideoCodec);
                list.Items.Add("-------------------");
            }
            else if (_file.FileType == 1)
            {
                list.Items.Add(MOJLocalization.Localize("Subtitle File"));
                list.Items.Add(MOJLocalization.Localize("Index") + ": " + _file.FileIndex);
                list.Items.Add(MOJLocalization.Localize("Language File") + ": " + _file.Language.Country);
                list.Items.Add("-------------------");
            }
        }
        private void FillComboBox()
        {
            _cmb_action.Items.Add(MOJLocalization.Localize("Keep File 1"));
            _cmb_action.Items.Add(MOJLocalization.Localize("Keep File 2"));
            _cmb_action.Items.Add(MOJLocalization.Localize("Skip Files"));
        }
        private void INIEvents()
        {
            OnResult += delegate { };
            _btn_Open1.Click += delegate { GlobalVars.TheHost.OpenFolder(_file1.FileFullPath); };
            _btn_Open2.Click += delegate { GlobalVars.TheHost.OpenFolder(_file2.FileFullPath); };
            _btn_ok.Click += ClickOK;
        }
        
        private void ClickOK(object sender, RoutedEventArgs e)
        {
            int x = _cmb_action.SelectedIndex;
            if (x == -1)
                return;

            _result = x;
            OnResult(this, new RoutedEventArgs());
        }

        public int Result
        { get { return _result; } }
        public event RoutedEventHandler OnResult;
    }

    public enum DuplicateResult
    {
        KeepFile1 = 0,
        KeepFile2 = 1,
        SkipThisFiles = 2,
    }
}
